package com.gitee.cirnochat.chatai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.cirnochat.chatai.mapper.AiChatMessageMapper;
import com.gitee.cirnochat.chatai.model.entity.AiChatMessage;
import com.gitee.cirnochat.chatai.service.AiChatMessageService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author FlanChan
* @description 针对表【ai_chat_message】的数据库操作Service实现
* @createDate 2025-06-06 16:22:23
*/
@Service
public class AiChatMessageServiceImpl extends ServiceImpl<AiChatMessageMapper, AiChatMessage>
    implements AiChatMessageService {

    @Override
    public List<AiChatMessage> getMessagesBySessionId(Long sessionId, Long userId) {
        return this.list(new LambdaQueryWrapper<>(AiChatMessage.class)
                .eq(AiChatMessage::getSessionId, sessionId)
                .orderByAsc(AiChatMessage::getCreateTime));
    }

    @Override
    @Async
    public void removeMessagesBySessionIdAsync(Long sessionId) {
        this.remove(new LambdaQueryWrapper<>(AiChatMessage.class)
                .eq(AiChatMessage::getSessionId, sessionId));
    }

    @Override
    public void saveMessage(AiChatMessage message) {
        this.save(message);
    }

    @Async
    @Override
    public void saveMessageAsync(AiChatMessage message) {
        this.saveMessage(message);
    }

    @Async
    @Override
    public void updateMessageRecordIdAsync(Long messageId, String recordId) {
        this.update(new LambdaUpdateWrapper<>(AiChatMessage.class)
                .eq(AiChatMessage::getId, messageId)
                .set(AiChatMessage::getRecordId, recordId));
    }
}




